/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sowrov.afrigis.tools.sqlite;

import java.util.ArrayList;

/**
 *
 * @author Sowrov
 */
public class DbTable {
    private String tableName;
    private ArrayList<TableColumn> columList;
    private ArrayList<String> columnNameList;

    public DbTable() {
        tableName = "";
        columList = new ArrayList<TableColumn>();
        columnNameList = new ArrayList<String>();
    }

    public DbTable(String tableName) {
        this.tableName = tableName;
        columList = new ArrayList<TableColumn>();
        columnNameList = new ArrayList<String>();
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public int getColumnCount(){
        return columList.size();
    }

    public TableColumn getColumnAt(int i){
        if(i>=columList.size() || i<0) {
            return null;
        }
        return columList.get(i);
    }

    public String getTableCreateStatement() {
        StringBuilder str = new StringBuilder(tableName);
        str.append("(");
        for (int i=0; i<columList.size(); i++) {
            if(i>0) {
                str.append(", ");
            }
            str.append(columList.get(i).toString());
        }
        str.append(")");
        return str.toString();
    }

    public void addColumn(TableColumn column) {
        this.columList.add(column);
        this.columnNameList.add(column.getName());
    }

    public void addColumn(String columnName) {
        this.columList.add(new TableColumn(columnName));
        this.columnNameList.add(columnName);
    }

    public void addIntegerColumn(String columnName){
        TableColumn column = new TableColumn(columnName);
        column.addAttribute("INTEGER");
        this.columList.add(column);
        this.columnNameList.add(columnName);
    }

    /**
     * add a primary key column
     * @param columnName name of the column
     * @param type example: INTEGER
     */
    public void addPrimaryKeyColumn(String columnName, String type){
        TableColumn column = new TableColumn(columnName);
        if (!type.equals("")) {
            column.addAttribute(type);
        }
        column.addAttribute("PRIMARY KEY");
        this.columList.add(column);
        this.columnNameList.add(columnName);
    }

    public void removeColumns() {
        this.columList.clear();
        this.columnNameList.clear();
    }

    public ArrayList<String> getColumnNames() {
        return this.columnNameList;
    }
}
